const Controller = require('egg').Controller

class AdminController extends Controller {

  async index() {
    const { ctx } = this
    let res = await ctx.service.admin.getAdminList()
    // console.log(res);
    if(res) {
      ctx.body = {
        code:20000,
        data:res
      }
    }else {
      ctx.body = {
        code:50000,
        data:'服务器异常'
      }
    }
  }

  async create() {
    try{
      const { ctx } = this
      let body = ctx.request.body.data
      console.log(body);
      await ctx.service.admin.postAdmin(body)
      ctx.body = {
        code:20000,
        data:'添加成功'
      }
    }catch(error){
      ctx.body = {
        code:50000,
        data:'服务器异常'
      }
    }
  }

  async update() {
    try{
      const { ctx } = this
      let id = ctx.params.id
      let newpassword = ctx.request.body.data
      await ctx.service.admin.putAdmin(id,newpassword)
      ctx.body = {
        code:20000,
        data:'修改成功'
      }
    }catch(error){
      ctx.body = {
        code:50000,
        data:'服务器异常'
      }
    }
  }

  async destroy() {
    try{
      const { ctx } = this
      let id = ctx.params.id
      await ctx.service.admin.delAdmin(id)
      ctx.body = {
        code:20000,
        data:'删除成功'
      }
    }catch(error){
      ctx.body = {
        code:50000,
        data:'服务器异常'
      }
    }
  }

}

module.exports = AdminController